<?php
namespace app\index\controller;

use \app\backend\model\Menu;
use app\backend\org\HeadTitle;
use \think\Db;
use app\backend\org\Crumbs;
use app\backend\org\Kv;

/**
 * 全时全程控制器
 */
class Fulltime  extends \think\Controller
{
    //全时全程fulltime页面接口
    public function index()
    {
        try {
            $menu_id = input('menu_id', '-1', 'htmlspecialchars');//第三个参数过滤
            $language = input("lang", "cn", "htmlspecialchars");
            if ($language == "cn") {
                $menu_name = "menu_name_cn";
                $content = "content_cn";
                $title = "title_cn";
            } elseif ($language == "en") {
                $menu_name = "menu_name_en";
                $content = "content_en";
                $title = "title_en";
            }
            $data = Menu::All(["p_id" => ["exp", "<>0"]]);
            //调用headtitle查title内容
            $head_title = HeadTitle::title($data, $menu_id, $language, "menu");
            //echo "<pre>";
            // print_r($head_title);exit;
            //调用kv类查kv画面内容
            $menu = Kv::Kv($menu_id);
            /*if($menu[0]["menu_name"]=="全时全程"){
                $menu[0]["menu_name"]="全时全程服务";
            }*/
            //return json($menu);exit;
            //echo "<pre>";
            //print_r($menu);exit;
            //全时全程的独立介绍
            $other_content = Db::query("select $title as title,$content as content,type_id from ls_other where `p_id`=".$menu[0]['menu_id']." and `delete_time` is null order by manual_sort ASC");
            //康得源等独立介绍
            $kxady = Db::query("select m.menu_id,m.$menu_name as menu_name,o.other_id,o.$title as title,o.$content as content,o.type_id from ls_menu m left join ls_other o on m.menu_id=o.p_id where m.p_id= ".$menu[0]['menu_id']." and m.delete_time is null and o.delete_time is null order by o.manual_sort");
            //return json($kxady);exit;
            //var_dump($kxady);exit;
            //修改铂金版为铂金版VIP服务
            foreach($kxady as $k=>$v){
                //echo 123;exit;
                if($v["title"]=="铂金版"){
                    $kxady[$k]["title"]="铂金版VIP服务";
                }
            }

            //获取面包屑
            $crumbs = Crumbs::crumbs($data, $menu_id, $language, "menu");
            // echo "<pre>";
            //print_r($crumbs);exit;
            //判断记录存不存在
            if (empty($menu)) {
                return json([
                    'code' => 2555,
                    'message' => '不存在这个id',
                ]);
            } else {
                return json([
                    'code' => 200,
                    'head_title' => $head_title,
                    'KV' => $menu[0],
                    'crumbs' => $crumbs,
                    'fulltime_content' => $other_content[0],
                    'kxady_content' => $kxady,
                    "imsg" => "全时全程-fulltime.html"
                ]);
            }
        } catch (\Exception $e) {
//			$this->_log2db('index::被 catch 了');
            print_r($e);
            return json([
                'code' => 2333,
                'imsg' => '被catch',
                'message' => '数据库查询失败 ' . $e->getMessage(),
            ]);
        }
    }

    //全时全程kxay页面接口
    public function kxady()
    {
        try {
            $menu_id = input('menu_id', '-1', 'htmlspecialchars');//第三个参数过滤
            $language = input("lang", "cn", "htmlspecialchars");
            if ($language == "cn") {
                $tab_name = "tab_name_cn";
                $list_title = "list_title_cn";
                $stage_title = "stage_title_cn";
                $content= "content_cn";
            } elseif ($language == "en") {
                $tab_name = "tab_name_en";
                $list_title = "list_title_en";
                $stage_title = "stage_title_en";
                $content= "content_en";
            }
            $data = Menu::All(["p_id" => ["exp", "<>0"]]);
            //调用headtitle查title内容
            $head_title = HeadTitle::title($data, $menu_id, $language, "menu");
            //echo "<pre>";
            // print_r($head_title);exit;
            //调用kv类查kv画面内容
            $menu = Kv::Kv($menu_id);
            //echo "<pre>";
            //print_r($menu);exit;

            //tab分类切换
            $tab = Db::query("select tab_id,p_id,$tab_name as tab_name,type_id from ls_tab where `p_id`={$menu[0]['menu_id']} and `delete_time` is null order by sort ASC ");

            //tab分类下面list表的内容
            $list = Db::query("select list_id,p_id,$list_title as list_title from ls_list where `delete_time` is null order by manual_sort ASC ");

            //stage表下面内容
            $stage = Db::query("select stage_id,p_id,$stage_title as stage_title,$content as content from ls_stage where `delete_time` is null order by `manual_sort` ASC ");
            //循环拼接三个表内容
            foreach ($tab as $k => $v) {
                foreach ($list as $a => $b) {
                    foreach($stage as $c => $d){
                        if($b["list_id"] == $d["p_id"]) {
                            if  (!isset($list[$a]["child"])) {
                                $list[$a]["child"] = [$d];
                            }
                            break;
                        }
                    }
                    if ($v["tab_id"] == $b["p_id"]) {
                        if (isset($tab[$k]["child"])) {
                            $tab[$k]["child"][] = $list[$a];
                        } else {
                            $tab[$k]["child"] = [$list[$a]];
                        }
                    }
                }
            }
            //  return json($tab);
            //获取面包屑
            $crumbs = Crumbs::crumbs($data, $menu_id, $language, "menu");
            // echo "<pre>";
            //print_r($crumbs);exit;
            //判断记录存不存在
            if (empty($menu)) {
                return json([
                    'code' => 2555,
                    'message' => '不存在这个id',
                ]);
            } else {
                return json([
                    'code' => 200,
                    'head_title' => $head_title,
                    'KV' => $menu[0],
                    'crumbs' => $crumbs,
                    'tab' => $tab,
                    "imsg" => "全时全程-康新源-kxy.html或者康安源-kay.html或者康得源-kdy.html "
                ]);
            }
        } catch (\Exception $e) {
//			$this->_log2db('index::被 catch 了');
            return json([
                'code' => 2333,
                'imsg' => '被catch',
                'message' => '数据库查询失败 ' . $e->getMessage(),
            ]);
        }
    }
}
